home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 15 / develop 15 code / Floating Windows / Sample / Source / menuDispatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-14  |  3.1 KB  |  135 lines  |  [TEXT/MPS ]

  1. #include <Types.h>
  2. #include <Desk.h>
  3. #include <Dialogs.h>
  4. #include <Events.h>
  5. #include <Files.h>
  6. #include <Menus.h>
  7.  
  8. #include "globals.h"
  9. #include "eventHandler.h"
  10. #include "fileMenuRoutines.h"
  11. #include "menuDispatch.h"
  12. #include "WindowExtensions.h"
  13.  
  14. void DoMenuCommand(long menuChoice)
  15. {
  16.     long    menuID;
  17.     long    menuItem;
  18.     short    daRef;
  19.     Str255    daName;
  20.     Boolean    editResult;
  21.     
  22.     menuID = (menuChoice & 0xFFFF0000) >> 16;
  23.     menuItem = menuChoice & 0x0000FFFF;
  24.     
  25.     switch (menuID) {
  26.         case rAppleMenuID:        if (menuItem == kAboutItem)
  27.                                     DoAbout();
  28.                                 else {
  29.                                     GetItem(GetMHandle(rAppleMenuID), (short) menuItem, &daName);
  30.                                     daRef = OpenDeskAcc(daName);
  31.                                 }
  32.                                 break;
  33.         case rFileMenuID:        HandleFileMenuItems((short) menuItem);
  34.                                 break;
  35.         case rEditMenuID:        editResult = SystemEdit((short) (menuItem - 1));
  36.                                 break;
  37.         case rFloatersMenuID:    HandlerFloaterMenuItems((short) menuItem);
  38.                                 break;
  39.     }
  40.     HiliteMenu(0);
  41. }
  42.  
  43. void DoAbout()
  44. {
  45.     short    alertResult;
  46.     
  47.     DeactivateFloatersAndFirstDocumentWindow();
  48.     alertResult = Alert(256, nil);
  49.     ActivateFloatersAndFirstDocumentWindow();
  50. }
  51.  
  52. void HandleFileMenuItems(short theItem)
  53. {
  54.     switch (theItem) {
  55.         case kNewItem:        CreateNewDocumentWindow();
  56.                             break;
  57.         case kOpenItem:        GetFileToOpen();
  58.                             break;
  59.         case kCloseItem:    CloseFirstDocumentWindow();
  60.                             break;
  61.         case kFindItem:        ShowFindDialog();
  62.                             break;
  63.         case kQuitItem:        gDone = true;
  64.                             break;
  65.     }
  66. }
  67.  
  68. void HandlerFloaterMenuItems(short theItem)
  69. {
  70.     Boolean        floaterVisible;
  71.     WindowRef    floaterPicked;
  72.     
  73.     switch (theItem) {
  74.         case kRecordingItem:    floaterPicked = gRecordingFloater;
  75.                                 break;
  76.         case kToolsItem:        floaterPicked = gToolsFloater;
  77.                                 break;
  78.     }
  79.     
  80.     floaterVisible = GetWindowVisible(floaterPicked);
  81.     if (floaterVisible == false)
  82.         ShowReferencedWindow(floaterPicked);
  83.     else
  84.         HideReferencedWindow(floaterPicked);
  85.     CheckItem(GetMHandle(rFloatersMenuID), theItem, !floaterVisible);
  86. }
  87.  
  88. void CreateNewDocumentWindow()
  89. {
  90.     WindowRef    newDocumentWindow;
  91.     OSErr        createWindowError;
  92.     
  93.     ValidateWindowList();
  94.     createWindowError = GetNewWindowReference(&newDocumentWindow, 128,
  95.                                                 (WindowRef) -1, gActivateEventHandler);
  96.     if (createWindowError == noErr) {
  97.         ShowReferencedWindow(newDocumentWindow);
  98.         EnableItem(GetMHandle(rFileMenuID), kCloseItem);
  99.     }
  100. }
  101.  
  102. void CloseFirstDocumentWindow()
  103. {
  104.     WindowRef    firstDocumentWindow;
  105.     
  106.     ValidateWindowList();
  107.     firstDocumentWindow = FrontNonFloatingWindow();
  108.     if (firstDocumentWindow != nil)
  109.         DisposeWindowReference(firstDocumentWindow);
  110.     if (FrontNonFloatingWindow() == nil)
  111.         DisableItem(GetMHandle(rFileMenuID), kCloseItem);
  112. }
  113.  
  114. void ShowFindDialog()
  115. {
  116.     DialogPtr    findDialog;
  117.     
  118.     DeactivateFloatersAndFirstDocumentWindow();
  119.     findDialog = GetNewDialog(257, nil, (WindowPtr) -1);
  120.     DisableItem(GetMHandle(rAppleMenuID), 0);
  121.     DisableItem(GetMHandle(rFileMenuID), 0);
  122.     DrawMenuBar();
  123. }
  124.  
  125. void DisposeFindDialog()
  126. {
  127.     DialogPtr    findDialog;
  128.     
  129.     findDialog = FrontWindow();
  130.     DisposeDialog(findDialog);
  131.     ActivateFloatersAndFirstDocumentWindow();
  132.     EnableItem(GetMHandle(rAppleMenuID), 0);
  133.     EnableItem(GetMHandle(rFileMenuID), 0);
  134.     DrawMenuBar();
  135. }